// 环形链表2

package Leetcode;

public class Solution142 {
    public ListNode detectCycle(ListNode head) {
        if (head == null)
            return null;
        ListNode slow = head;
        ListNode fast = head;
        do {
            if (slow.next == null || fast.next == null || fast.next.next == null)
                return null;
            slow = slow.next;
            fast = fast.next.next;
        } while (slow != fast);
        // 完成while循环后两者相遇，将slow放置开头进行移动
        slow = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}
